package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbClazzBook;
import com.example.ymm.modules1.entity.my.user.ViewBook;
import com.example.ymm.modules1.mapper.my.user.DbClazzBookMapper;
import com.example.ymm.modules1.service.dto.DbClazzBookQueryCriteria;
import com.example.ymm.modules1.service.user.DbClazzBookService;
import com.example.ymm.util.QueryHelpPlus;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DbClazzBookServiceImpl extends BaseServiceImpl<DbClazzBookMapper, DbClazzBook> implements DbClazzBookService {

    @Override
    public TableList<ViewBook> list(DbClazzBookQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<ViewBook> maps = baseMapper.getBookList(criteria);
        PageInfo<ViewBook> pageInfo = new PageInfo<>(maps);
        TableList<ViewBook> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    @Override
    public TableList<ViewBook> bookClazzNameList(DbClazzBookQueryCriteria criteria, YmPageDto ymPage) {
        getPage(ymPage);
        List<ViewBook> maps = baseMapper.getBookClazzNameList(criteria);
        PageInfo<ViewBook> pageInfo = new PageInfo<>(maps);
        TableList<ViewBook> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    public List<ViewBook> queryAll(DbClazzBookQueryCriteria criteria) {
        List<ViewBook> maps = baseMapper.getBookList(criteria);
//        List<DbClazzBook> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbClazzBook.class, criteria));
        return maps;
    }

    @Override
    public List<DbClazzBook> getClazzBook(DbClazzBookQueryCriteria criteria) {
        List<DbClazzBook> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbClazzBook.class, criteria));
        return result;
    }

    @Override
    public void setBooks(String clazzId, String bookIds) {
        String[] split = bookIds.split(",");

        List<DbClazzBook> list=new ArrayList<>();
        for(String s:split){
            LambdaQueryWrapper<DbClazzBook> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DbClazzBook::getBookId,s);
            remove(queryWrapper);
            DbClazzBook clazzBook=new DbClazzBook();
            clazzBook.setBookId(s);
            clazzBook.setClazzId(clazzId);
            list.add(clazzBook);
        }
        saveOrUpdateBatch(list);
    }

    @Override
    public void setBookByClazzIds(String clazzIds, String bookId) {
        String[] split = clazzIds.split(",");

        List<DbClazzBook> list=new ArrayList<>();
        for(String s:split){
            LambdaQueryWrapper<DbClazzBook> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DbClazzBook::getBookId,bookId);
            remove(queryWrapper);
            DbClazzBook clazzBook=new DbClazzBook();
            clazzBook.setBookId(bookId);
            clazzBook.setClazzId(s);
            list.add(clazzBook);
        }
        saveOrUpdateBatch(list);
    }
}
